// pages/category/category.js

var http = require("../../utils/http.js");
var config = require("../../utils/config.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    selIndex: 0,
    categoryList:[],
    productList: [],
    categoryImg: '',
    prodid:'',
    imagesList:[
      {
        imagetext:"天天领红包",
        imagePath:"http://rrxbq3qzt.hn-bkt.clouddn.com/%E7%8E%B0%E9%87%91%E7%BA%A2%E5%8C%85.png"
      },
      {
        imagetext:"附近拼单",
        imagePath:"http://rrxbq3qzt.hn-bkt.clouddn.com/%E9%99%84%E8%BF%91.png"
      },
      {
        imagetext:"加福利官",
        imagePath:"http://rrxbq3qzt.hn-bkt.clouddn.com/%E5%8A%A0%E5%A5%BD%E5%8F%8B.png"
      },
      {
        imagetext:"推荐",
        imagePath:"http://rrxbq3qzt.hn-bkt.clouddn.com/%E9%A3%9F%E7%89%A9.png"
      },
      {
        imagetext:"米粉面条",
        imagePath:"http://rrxbq3qzt.hn-bkt.clouddn.com/%E7%B1%B3%E7%B2%89%E9%9D%A2%E9%A6%86.png"
      },
      {
        imagetext:"汉堡披萨",
        imagePath:"http://rrxbq3qzt.hn-bkt.clouddn.com/%E6%B1%89%E5%A0%A1%E6%8A%AB%E8%90%A8.png"
      },
      {
        imagetext:"饮品甜点",
        imagePath:"http://rrxbq3qzt.hn-bkt.clouddn.com/%E7%8F%8D%E7%8F%A0%E5%A5%B6%E8%8C%B6.png"
      },
      {
        imagetext:"炒饭拌饭",
        imagePath:"http://rrxbq3qzt.hn-bkt.clouddn.com/%E8%9B%8B%E7%82%92%E9%A5%AD.png"
      },
      {
        imagetext:"炸鸡烧烤",
        imagePath:"http://rrxbq3qzt.hn-bkt.clouddn.com/%E7%82%B8%E9%B8%A1%E7%83%A7%E7%83%A4.png"
      },
      {
        imagetext:"饺子粥饼",
        imagePath:"http://rrxbq3qzt.hn-bkt.clouddn.com/%E9%A5%BA%E5%AD%90.png"
      },
      {
        imagetext:"川菜香锅",
        imagePath:"http://rrxbq3qzt.hn-bkt.clouddn.com/%E5%B7%9D%E8%8F%9C-18.png"
      },
      {
        imagetext:"日韩料理",
        imagePath:"http://rrxbq3qzt.hn-bkt.clouddn.com/%E6%97%A5%E9%9F%A9%E6%96%99%E7%90%86.png"
      },
      

    ],
    goodsList: [
      {
      goodsname: "多肉阳光橙",
      goodsprice:"6.9",
      imagePath:"http://rrxbq3qzt.hn-bkt.clouddn.com/%E5%A4%9A%E8%82%89%E9%98%B3%E5%85%89%E6%A9%99.jpg"
      },
      {
        goodsname: "原味螺蛳粉",
        goodsprice:"8.9",
        imagePath: "http://rrxbq3qzt.hn-bkt.clouddn.com/R-C.jfif"
      },
      {
        goodsname: "香辣鸡腿堡+墨西哥鸡肉卷+可乐250ml",
        goodsprice:"13.9",
        imagePath: "http://rrxbq3qzt.hn-bkt.clouddn.com/R-C%20%281%29.jfif"
      },
      {
        goodsname: "韩式拌饭",
        goodsprice:"10.58",
        imagePath: "http://rrxbq3qzt.hn-bkt.clouddn.com/n4125983_158884229722438.jpg"
      },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var ths = this;
    //加载分类列表
    var params = {
      url: "/category/categoryInfo",
      method: "GET",
      data: {
        parentId: ''
      },
      callBack: function (res) {
        // console.log(res);
        ths.setData({
          categoryImg: res[0].pic,
          categoryList: res,
        });
        ths.getProdList(res[0].categoryId)
      }
    };
    http.request(params);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 分类点击事件
   */
  onMenuTab: function (e) {
    console.log(e);
        var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    // this.getProdList(id);
    this.getProdList(this.data.categoryList[index].categoryId);
    this.setData({
      categoryImg: this.data.categoryList[index].pic,
      selIndex: index
    });
  },

  // 跳转搜索页
  toSearchPage: function () {
    wx.navigateTo({
      url: '/pages/search-page/search-page',
    })
  },
  getProdList(categoryId) {
    //加载分类列表
    var params = {
      url: "/prod/pageProd",
      method: "GET",
      data: {
        categoryId: categoryId
      },
      callBack: (res) => {
        // console.log(res);
        this.setData({
          productList: res.records,
        });
      }
    };
    http.request(params);
  },

  //跳转领红包页、加福利官页
topindan: function() {
  wx.navigateTo({
    url: '/pages/pindan/pindan',
  })
},
//跳转附近拼单页
tonear: function() {
  wx.navigateTo({
    url: '/pages/near/near',
  })
},
//跳转定位页
toLocation: function() {
  wx.navigateTo({
    url: '/pages/editAddress/editAddress',
  })
},
toguowu: function () {
  wx.navigateTo({
    url: '/pages/shangpin/shangpin',
  })
},

toTuijian: function() {
  wx.showToast({
    icon:"none",
    title: '该功能未开源'
  })
},

//跳转商品详情页
  toProdPage: function (e) {
    var prodid = e.currentTarget.dataset.prodid;
    wx.navigateTo({
      url: '/pages/prod/prod?prodid=' + prodid,
    })
  },
  
})

